package com.ehotting.edsta.mqbd.reliability.http.caller;

import com.alibaba.fastjson.JSON;
import com.ehotting.eaf.netty.loggers.NettyMsgReceiveLogger;
import com.ehotting.edsta.mqbd.api.enums.SysModuleEnum;
import com.ehotting.edsta.mqbd.reliability.api.bean.MQReliabilityData;
import com.ehotting.edsta.mqbd.reliability.api.domain.AnsysRunnerModel;
import com.ehotting.edsta.mqbd.reliability.api.domain.SimulateResultDto;
import com.ehotting.edsta.mqbd.reliability.http.hprose.IAnsysRunner;
import com.ehotting.mqbd.api.bean.MQDataAble;
import com.ehotting.mqbd.api.bean.solver.SolverStateBean;
import com.ehotting.mqbd.api.enums.SendTypeEnum;
import com.ehotting.mqbd.core.call.CallerAbstract;
import hprose.client.HproseClient;
import hprose.client.HproseHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;


public class ReliabilityCaller extends CallerAbstract {

    private static Logger logger = LoggerFactory.getLogger(ReliabilityCaller.class);

    @Autowired
    private NettyMsgReceiveLogger nettyMsgReceiveLogger;

    @Override
    public void doAction(SolverStateBean serverState, MQDataAble data)  {

        logger.info("Start doAction.");
        long startTime = System.currentTimeMillis();
        MQReliabilityData mqData = (MQReliabilityData) data;
        logger.info("mqData.getRequest().TimeOut:{} s,mqData.getRequest().WaitFor:{} s",mqData.getRequest().TimeOut,mqData.getRequest().WaitFor);


        IAnsysRunner ansysRunner = createIAnsysRunner( serverState, mqData);
        logger.info("Start ansysRunner.Run.");
        String result = ansysRunner.Run(createRequestObject(mqData));
        logger.info("finish ansysRunner.Run.");

        SimulateResultDto resultDto = new SimulateResultDto(mqData.getRequest().ResultName,result);
        nettyMsgReceiveLogger.info(JSON.toJSONString(resultDto));

        setResultData(mqData,resultDto);
        logger.info("CostTime:{}",(System.currentTimeMillis()-startTime));
        logger.info("Finish doAction.");
    }

    @Override
    public void validData(MQDataAble mqDataAble) {

    }

    private void setResultData(MQReliabilityData mqData,SimulateResultDto resultDto){
        mqData.setResponse(resultDto);
        mqData.setLogResponse(false);
        mqData.setSendRequest(false);
        mqData.setSendType(new SendTypeEnum[]{SendTypeEnum.REDIS_RESPONSE_BODY});
    }


    private Map<String, Object> createRequestObject(MQReliabilityData mqData){
        AnsysRunnerModel model = mqData.getRequest();
        String modelJsonStr = JSON.toJSONString(model);
        Map<String, Object> requestObject = JSON.parseObject(modelJsonStr, Map.class);
        return requestObject;
    }

    private IAnsysRunner createIAnsysRunner(SolverStateBean serverState,MQReliabilityData mqData){
        HproseClient client = new HproseHttpClient();
        client.setTimeout(mqData.getRequest().TimeOut * 1000);
        client.useService(replaceUrl(serverState,mqData.getRequestUrl()));

        IAnsysRunner ansysRunner = client.useService(IAnsysRunner.class);
        return ansysRunner;
    }


    @Override
    public String getModuleName() {
        return SysModuleEnum.RELIABILITY.getName();
    }


}
